package com.cskaoyan.service.wx;

import com.cskaoyan.bean.Collect;
import com.cskaoyan.bean.CollectExample;
import com.cskaoyan.bean.collect.CollectData;
import com.cskaoyan.bean.collect.CollectInfo;
import com.cskaoyan.mapper.CollectMapper;
import com.cskaoyan.util.UserInfoUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author xudengfeng
 * @date 2020/12/8 16:42
 */

@Service
public class CollectServiceImpl implements CollectService {

    @Autowired
    CollectMapper collectMapper;

    @Override
    public CollectData getCollectList(Byte type, Integer page, Integer size) {

        Integer userId = UserInfoUtils.getUserId();

        PageHelper.startPage(page, size);
      /*  CollectExample example = new CollectExample();
        example.setOrderByClause("id" + " " + "desc");
        CollectExample.Criteria criteria = example.createCriteria();
        if (type != null) {
            criteria.andTypeEqualTo(type);
        }


        criteria.andDeletedEqualTo(false);
        criteria.andUserIdEqualTo(userId);

        List<Collect> collects = collectMapper.selectByExample(example);*/
        //过滤取消了收藏的
        Boolean deleted = false;
        List<CollectInfo> collectInfos = collectMapper.selectCollectInfo(type, deleted, userId);

        CollectData collectData = new CollectData();
        collectData.setCollectList(collectInfos);

        PageInfo<CollectInfo> collectPageInfo = new PageInfo<>(collectInfos);
        long total = collectPageInfo.getTotal();

        collectData.setTotalPages((int) total);
        return collectData;
    }

    //加入 或 取消收藏
    //需要用户ID
    @Override
    public String addOrDeleteCollect(Map map) {

        Integer type = (Integer) map.get("type");
        byte b = type.byteValue();

        Integer valueId = (Integer) map.get("valueId");
        CollectExample example = new CollectExample();
        CollectExample.Criteria criteria = example.createCriteria();
        Integer userId = UserInfoUtils.getUserId();
        criteria.andUserIdEqualTo(userId);
        criteria.andTypeEqualTo(b);
        criteria.andValueIdEqualTo(valueId);
        criteria.andDeletedEqualTo(false);
        //只可能有一个值或空
        List<Collect> collects = collectMapper.selectByExample(example);

        //收藏列表为空,需要插入数据
        //add
        if (collects.size() == 0) {
            Collect collect = new Collect();

            collect.setUserId(userId);
            collect.setDeleted(false);
            Date date = new Date();
            collect.setAddTime(date);
            collect.setUpdateTime(date);
            collect.setType((byte) 0);
            collect.setValueId(valueId);
            collectMapper.insertSelective(collect);
            return "add";
        }

        //delete
        Collect collect = collects.get(0);
        collect.setDeleted(true);
        collectMapper.updateByPrimaryKeySelective(collect);
        return "delete";
    }
}
